package com.ruoyi.mentor.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.mentor.bo.SpotAddBo;
import com.ruoyi.mentor.bo.SpotEditBo;
import com.ruoyi.mentor.bo.SpotQueryBo;
import com.ruoyi.mentor.domain.Instructor;
import com.ruoyi.mentor.domain.Spot;
import com.ruoyi.mentor.domain.Subject;
import com.ruoyi.mentor.mapper.SpotMapper;
import com.ruoyi.mentor.service.ISpotService;
import com.ruoyi.mentor.vo.ExportSpotVo;
import com.ruoyi.mentor.vo.ExportStudentVo.ExportStudentVo;
import com.ruoyi.mentor.vo.InstructorVo;
import com.ruoyi.mentor.vo.SpotVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 招生名额Service业务层处理
 *
 * @author LH
 * @date 2021-05-21
 */
@Service
public class SpotServiceImpl extends ServiceImpl<SpotMapper, Spot> implements ISpotService {

    @Autowired
    InstructorServiceImpl instructorService;

    @Autowired
    SubjectServiceImpl subjectService;

    @Override
    public SpotVo queryById(Long id){
        return getVoById(id, SpotVo.class);
    }

    @Override
    public TableDataInfo<SpotVo> queryPageList(SpotQueryBo bo) {
        PagePlus<Spot, SpotVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), SpotVo.class);

        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<ExportSpotVo> queryList(SpotQueryBo bo) {
        List<ExportSpotVo> collect = listVo(buildQueryWrapper(bo), SpotVo.class).stream().map(item -> {
            ExportSpotVo exportSpotVo = new ExportSpotVo();
            Long insId = item.getInsId();
            Long subId = item.getSubId();
            Instructor instructor = instructorService.getById(insId);
            exportSpotVo.setCheckedNumber(item.getCheckedNumber());
            exportSpotVo.setPgNumber(item.getPgNumber());
            exportSpotVo.setExamineStatus(item.getExamineStatus());
            exportSpotVo.setYear(item.getYear());
            exportSpotVo.setInsName(instructor.getInsName());
            Subject subject = subjectService.getById(subId);
            exportSpotVo.setSubName(subject.getSubName());

            return exportSpotVo;
        }).collect(Collectors.toList());
        return collect;

    }

    private LambdaQueryWrapper<Spot> buildQueryWrapper(SpotQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Spot> lqw = Wrappers.lambdaQuery();
        lqw.eq(StrUtil.isNotBlank(bo.getYear()), Spot::getYear, bo.getYear());
        lqw.eq(bo.getInsId()!=null, Spot::getInsId, bo.getInsId());
        lqw.eq(bo.getSubId()!=null, Spot::getSubId, bo.getSubId());
        lqw.eq(bo.getPgNumber() != null, Spot::getPgNumber, bo.getPgNumber());
        lqw.orderByDesc(Spot::getYear);
        lqw.orderByDesc(Spot::getInsId);
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(SpotAddBo bo) {
        Spot add = BeanUtil.toBean(bo, Spot.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(SpotEditBo bo) {
        Spot update = BeanUtil.toBean(bo, Spot.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(Spot entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }

    /**
     * 获取该导师的该专业下的招生信息
     * @param insId
     * @param subId
     * @return
     */
    @Override
    public Spot getLastSpot(Long insId, Long subId) {
        List<Spot> list = baseMapper.getLastSpot(insId,subId);
        if (list==null||list.size()==0){
            return null;
        }else {
            return list.get(0);
        }
    }

    /**
     * 根据导师id获取该导师下，今年，招生未招满的学科信息
     *
     * @param insId
     * @return
     */
    @Override
    public List<InstructorVo.SubVo> getSubjectList(Long insId) {
        List<InstructorVo.SubVo> subjectList = baseMapper.getSubjectList(insId);
        return subjectList;
    }

    public Spot getSpot(Long insId, Long subId) {
        Spot one = getOne(new QueryWrapper<Spot>().eq("ins_id", insId).eq("sub_id", subId).eq("year", Calendar.getInstance().get(Calendar.YEAR)));
        return one;
    }

    public List<Map<String, Object>> getNotFullSpot() {
        return baseMapper.getNotFullSpot();
    }

}
